{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d61c99ba-5349-472b-a5bd-f54964d27f17",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Querying the Network Graph with Natural language\n",
    "\n",
    "This notebook shows how you can ask questions in natural language about the data in the graph database using the Neptune\n",
    "Langchain integration and Amazon Bedrock anthropic claude v2 model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60a09cea-ae76-45d0-9dc1-860fc3ad833f",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "!pip install langchain --quiet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb336cc7-2316-43a3-ac3d-6984b08ef228",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import warnings\n",
    "import langchain\n",
    "# langchain.__version__\n",
    "import boto3\n",
    "from langchain.chat_models import BedrockChat\n",
    "from langchain.chains import NeptuneOpenCypherQAChain\n",
    "from langchain.graphs import NeptuneGraph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a4b753d-6e9c-4f62-86b5-259165ff9f91",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "host = \"<your_neptune_host_endpoint>\" # e.g. cluter.cluster-xxxxxxxx.region.neptune.amazonaws.com\n",
    "port = 8182\n",
    "use_https = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4bc8717c-1d03-442d-9517-6adaa8768d09",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "graph = NeptuneGraph(host=host, port=port, use_https=use_https)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e5bd3a5-02d5-4cc6-8caa-df08659aaed7",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "client = boto3.client('bedrock-runtime', region_name='us-west-2')\n",
    "\n",
    "\n",
    "llm = BedrockChat(\n",
    "    model_id = \"anthropic.claude-v2\",\n",
    "    client = client\n",
    ")\n",
    "\n",
    "chain = NeptuneOpenCypherQAChain.from_llm(\n",
    "    llm = llm, graph = graph, verbose = True, top_K = 10, return_intermediate_steps=True, return_direct=False\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22910c4b-79de-440d-a2a2-0c4409bd4423",
   "metadata": {},
   "source": [
    "# Explore edges"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "845025be-87a9-4aaa-8855-8b0bb17010ad",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "chain.run(\"how many edges between the users and the cells in the graph?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e8cfaf2-761a-48d3-b704-e058a21d679b",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "chain.run(\"how many vertexes of type user\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b4dabd3-6369-4853-a767-359babef7be1",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "chain.run(\"how many nodes of type cell\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f53149c-23ca-4702-a32d-33f4e4a662b6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "chain.run(\"how many vertexes of type gnodeb\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d658504-0c87-4fe5-8e7c-0581d828f5b8",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "chain.run(\"list the two types of edges\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69b7fc44-b5a1-464f-b414-363ac7e48ac1",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "chain.run(\"count the edges between the cell and gnodeb ?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75711c07-ee2b-44c1-be42-bd2bd514c622",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "chain.run(\"count how many edges for cell_relatedto_gnodeb and user_live_cell\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dafb88cc-6969-45ec-bc6c-4329dd5ca092",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "chain.run(\"what are the types of vertex ?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "acb64772-b929-45dd-8402-4ba82ae93cfe",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "chain.run(\"to which cells the user_1500 is connceted\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aee5eb2b-c543-4b86-94bb-86254c097fa6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
